package com.library.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.library.dao.BookTitle;
import com.library.dao.DAOFactory;
import com.library.dao.MySQLDAOFactory;
import com.library.dao.User;
import com.library.dao.UserDAO;

/**
 * Servlet implementation class ShowBorrowedBook
 */
@WebServlet("/ShowBorrowedBook")
public class ShowBorrowedBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowBorrowedBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User instantUser = (User)session.getAttribute("instantUser");
		
		DAOFactory factory = new MySQLDAOFactory();
		UserDAO userDAO = factory.getUserDAO();
		
		ArrayList<BookTitle> borrowedBook = new ArrayList<BookTitle>();
		borrowedBook = userDAO.getBorrowedBook(instantUser.getUserId(), Resource.CONTINUOUS);
		Integer noBook = userDAO.NoOfBorrowtedBook(instantUser.getUserId(),Resource.CONTINUOUS);	
		boolean right = userDAO.checkOverdateBook(instantUser.getUserId());
		String notification = "";
		if (noBook == Resource.MAX_BOOK)
			notification = Resource.NOTIFICATION_XBORROW+" Vì bạn đã mượn tối đa số sách có thể";
		else if (right)
			notification = Resource.NOTIFICATION_XBORROW+" Vì bạn còn sách hết hạn mà chưa trả";
		else if (!right && (noBook < Resource.MAX_BOOK))
			notification = Resource.NOTIFICATION_BORROW;
		//request.setAttribute("instantUser", user);
		request.setAttribute("borrowedBook", borrowedBook);
		request.setAttribute("nobook", noBook);
		request.setAttribute("notification", notification);
		String address = "user/userborrowedbook.jsp";				
		 RequestDispatcher dispatcher =
			      request.getRequestDispatcher(address);
			    dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
